package com.atguigu.tingshu.user.pattern.factory;

import com.atguigu.tingshu.user.pattern.DeliveryPolicy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class DeliveryPolicyFactory {
    /**
     * 自动发现 策略接口DeliveryPolicy下所有实现类对象 注入Map中 Map的Key是策略实现类对象BeanID Map的Value是策略实现类对象
     */
    @Autowired
    private Map<String, DeliveryPolicy> deliveryPolicyMap;

    public DeliveryPolicy getDeliveryPolicy(String itemType) {
        if (deliveryPolicyMap.containsKey(itemType)) {
            return deliveryPolicyMap.get(itemType);
        }
        throw new RuntimeException("未查询到该商品类型");
    }
}
